//
//  AYMModifyPSWController.m
//  QuickCure
//
//  Created by 徐绩 on 16/5/6.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "AYMModifyPSWController.h"

@interface AYMModifyPSWController ()<UITextFieldDelegate>

@property (nonatomic, weak) UIView *basicView;

@property (nonatomic, weak) UIView *newsView;

@property (nonatomic, weak) UIView *recurView;

@property (nonatomic, weak) UITextField *basicTextField;

@property (nonatomic, weak) UITextField *newsTextField;

@property (nonatomic, weak) UITextField *recurTextField;

@property (nonatomic, weak) UIButton *okButton;

@end

@implementation AYMModifyPSWController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = random(236, 236, 236, 255);
    self.title = @"重置密码";
    [self setUpUi];
    
    
}

- (void)setUpUi {
    
    //添加view
    UIView *basicView = [self addPawView];
    self.basicView = basicView;
    
    UIImageView *basicImageView = [self addChildImage:self.basicView];
    //设置约束
    [basicView makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.view).offset(50);
        make.trailing.equalTo(self.view).offset(-50);
        make.width.equalTo(self.view.frame.size.width - 100);
        make.height.equalTo(40);
        make.top.equalTo(self.view).offset(30);
    }];
    [self setsFrameWithImageView:basicImageView andRelativeView:basicView];
    
    //添加文本框
    UITextField *basicTextField = [self addChildTextField:self.basicView];
    self.basicTextField = basicTextField;
    basicTextField.placeholder = @"请输入原密码";
    
    
    
    [self setsFrameWithTextField:basicTextField andRelativeView:basicView Relativeimage:basicImageView];
    
    //新密码
    UIView *newsView = [self addPawView];
    self.newsView = newsView;
    [self setsFrameWithView:newsView andRelativeView:basicView];
    
    
    //图片框
    UIImageView *newImageView = [self addChildImage:self.newsView];
    [self setsFrameWithImageView:newImageView andRelativeView:newsView];
    
    
    //添加文本框
    UITextField *newsTextField = [self addChildTextField:self.newsView];
    self.newsTextField = newsTextField;
    newsTextField.placeholder = @"请输入新密码";

    
    [self setsFrameWithTextField:newsTextField andRelativeView:newsView Relativeimage:newImageView];
    
    
    
    
    //再次输入密码
    UIView *recurView = [self addPawView];
    self.recurView = recurView;
    [self setsFrameWithView:recurView andRelativeView:newsView];
    
    
    UIImageView *recurImageView = [self addChildImage:self.recurView];
    [self setsFrameWithImageView:recurImageView andRelativeView:recurView];
    
    //添加文本框
    UITextField *recurTextField = [self addChildTextField:self.recurView];
    self.recurTextField = recurTextField;
    recurTextField.placeholder = @"请再次输入新密码";
    [self setsFrameWithTextField:recurTextField andRelativeView:recurView Relativeimage:recurImageView];
    
    
    //添加点击按钮
    //添加点击按钮
    UIButton *okButton = [[UIButton alloc] init];
    self.okButton = okButton;
    okButton.enabled = NO;
    [okButton addTarget:self action:@selector(didOkBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [okButton setTitle:@"确定" forState:UIControlStateNormal];
    okButton.backgroundColor = random(24, 188, 187, 255);
    okButton.titleLabel.font = [UIFont systemFontOfSize:homeFont];
    
    
    [self.view addSubview:okButton];
    
    [okButton makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(recurView.bottom).offset(40);
        make.leading.equalTo(recurView.leading);
        make.trailing.equalTo(recurView.trailing);
        make.height.equalTo(40);
        
    }];
    
}

#pragma mark - 按钮点击事件
- (void)didOkBtnClick {
    
    if (self.newsTextField.text != self.recurTextField.text) {
        
        [SVProgressHUD showInfoWithStatus:@"两次密码不正确"];
        self.recurTextField.text = nil;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismissWithDelay:0.5];
            
            
        });
        return;
    }
    
    
    
    [SVProgressHUD showInfoWithStatus:@"密码已经重置"];
    
    self.basicTextField.text = nil;
    self.newsTextField.text = nil;
    self.recurTextField.text = nil;
    
    [SVProgressHUD dismissWithDelay:0.5];
    [self.view endEditing:YES];
    
    
}

#pragma mark - 设置子控件
- (UIView *)addPawView {
    
    UIView *pawView = [[UIView alloc] init];
    
    //设置背景颜色
    pawView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:pawView];
    
    return pawView;
}

- (UIImageView *)addChildImage:(UIView *)basicView {
    
    UIImageView *ImageView = [[UIImageView alloc] init];
    ImageView.image = [UIImage imageNamed:@"jinggao"];
    [basicView addSubview:ImageView];
    
    return ImageView;
}

- (UITextField *)addChildTextField:(UIView *)basicView {
    
    UITextField *textField = [[UITextField alloc] init];
    textField.font = [UIFont systemFontOfSize:homeFont];
    textField.delegate = self;
    textField.layer.cornerRadius = 5;
    [textField addTarget:self action:@selector(didTextFieldClick) forControlEvents:UIControlEventEditingChanged];
    textField.layer.borderWidth = 1;
    textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [basicView addSubview:textField];
    
    
    return textField;
}

#pragma mark - 设置frame
//view
- (void)setsFrameWithView:(UIView *)basicView andRelativeView:(UIView *)relativeView {
    //设置约束
    [basicView makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.view).offset(50);
        make.trailing.equalTo(self.view).offset(-50);
        make.width.equalTo(self.view.frame.size.width - 100);
        make.height.equalTo(40);
        make.top.equalTo(relativeView.bottom).offset(30);
    }];
    
    
}

//图片
- (void)setsFrameWithImageView:(UIImageView *)basicImage andRelativeView:(UIView *)relativeView {
    
    [basicImage makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(relativeView).offset(5);
        make.top.equalTo(relativeView).offset(12);
        make.bottom.equalTo(relativeView).offset(-12);
        make.width.equalTo(20);
        
    }];
    
}

- (void)setsFrameWithTextField:(UITextField *)basicTextField andRelativeView:(UIView *)relativeView Relativeimage:(UIImageView *)relativeImage {
    
    [basicTextField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(relativeView).offset(5);
        make.bottom.equalTo(relativeView).offset(-5);
        make.leading.equalTo(relativeImage.trailing).offset(10);
        make.trailing.equalTo(relativeView).offset(-10);
    }];
    
    
}

#pragma mark - 触摸事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];    
}

#pragma mark - textField的值改变方法
- (void)didTextFieldClick {
    
    
    BOOL textFieldBasic = self.basicTextField.text.length > 0;
    BOOL textFieldNews = self.newsTextField.text.length > 0;
    BOOL textFieldRecur = self.recurTextField.text.length > 0;
    
    if (textFieldBasic && textFieldNews && textFieldRecur) {
        self.okButton.enabled = YES;
    }
    
    
    
    
}

#pragma mark - textField的代理方法
//开始编辑的时候
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    textField.keyboardType = UIKeyboardTypeNumberPad;
    [textField becomeFirstResponder];
    
}


@end
